import COS from 'cos-js-sdk-v5'
import { gbs } from '@/utils'

const time = new Date().getTime();

function upload(file, folder, callback) {
	if(!folder) {
		folder = 'public/'
	}
	let cos = new COS({
		SecretId: gbs.SecretId,
		SecretKey: gbs.SecretKey,
	})
	let url = cos.sliceUploadFile({
		Bucket: gbs.Bucket,
		Region: gbs.Region,
		Key: folder + file.name,
		Body: file,
		onHashProgress: (progressData) => {
			console.log('校验中', JSON.stringify(progressData));
		},
		onProgress: (progressData) => {
			callback({
				url: '',
				percent: progressData.percent,
				speed: progressData.speed
			})
		},
		function(err, data) {
			console.log(data, 'ok')
		},
	}).then(res => {
		callback({
			url: 'http://' + res.Location,
			percent: 1,
			speed: 0
		})
	})
}

export {
	upload
}